package com.supervise.demos.web;

import cn.hutool.db.nosql.redis.RedisDS;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import redis.clients.jedis.Jedis;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuhuc
 * @data 2024/4/12 - 13:02
 */
public class Demo {
    public static void main(String[] args) {
        String seturl = "https://www.cbirc.gov.cn/branch/beijing/view/pages/common/ItemList.html?itemId=";
        List<User> list = new ArrayList<>();
        //获取全部组织
        String url = "https://www.cbirc.gov.cn/cn/static/data/OrgInfo/SelectOrg/data_.json";
        final byte[] hosts = HttpRequest.get(url).execute().bodyBytes();
        String data = new String(hosts, StandardCharsets.UTF_8);
        final JSONArray data1 = JSON.parseObject(data).getJSONArray("data");
        for (Object o : data1) {
            User user = new User();
            final String orgName = JSON.parseObject(o.toString()).getString("orgName");
            final String remark2 = JSON.parseObject(o.toString()).getString("agencyShortlink");
            //排除总会
            if (orgName.contains("总会")) {
                continue;
            }
            user.setName(orgName);
            //获取组织的itemId
            String url1 = "https://www.cbirc.gov.cn/cn/static/data/item/getOrgWebItemList/data_orgname=";
            final byte[] hosts2 = HttpRequest.get(url1 + remark2 + ".json").execute().bodyBytes();
            String data2 = new String(hosts2, StandardCharsets.UTF_8);
            final JSONArray subItemslist = JSON.parseObject(data2).getJSONObject("data").getJSONArray("subItemslist");
            for (Object o1 : subItemslist) {
                final String itemName = JSON.parseObject(o1.toString()).getString("itemName");
                if (itemName.contains("机构概况")) {
                    final JSONArray subItemslist1 = JSON.parseObject(o1.toString()).getJSONArray("subItemslist");
                    for (Object o2 : subItemslist1) {
                        final String itemName1 = JSON.parseObject(o2.toString()).getString("itemName");
                        if (itemName1.contains("联系方式")) {
                            final String itemId = JSON.parseObject(o2.toString()).getString("itemId");
                            user.setItemId(itemId);
                            break;
                        }
                    }

                }
            }
            //获取文档
            String url2 = "https://www.cbirc.gov.cn/cn/static/data/DocInfo/SelectDocEntityByItemId/data_itemId=";
            final byte[] hosts3 = HttpRequest.get(url2 + user.getItemId() + ".json").execute().bodyBytes();
            String data3 = new String(hosts3, StandardCharsets.UTF_8);
            String docClob = JSON.parseObject(data3).getJSONObject("data").getString("docClob");
            user.setContent(docClob);

            user.setSite(seturl + user.getItemId() + "&itemUrl=ItemListRightArticle.html&itemName=%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F");
            list.add(user);
        }

        //拼装数据类型


        Map<String, String> map1 = new HashMap<>();
        for (User user : list) {
            map1.put(user.getName(), JSON.toJSONString(user));
        }

        //存到redis
        Jedis jedis = RedisDS.create().getJedis();
        jedis.hset("Fin_manager_page_23", map1);
        jedis.close();
    }

}
